package com.hk.housekeeping.order.controller.order;

import com.hk.housekeeping.order.entity.Order;
import com.hk.housekeeping.order.service.OrderDetailServ;
import com.hk.housekeeping.order.service.impl.OrderDetailServImpl;
import com.hk.housekeeping.order.utils.PageUtils;
import com.hk.housekeeping.order.utils.ResponseCode;
import com.hk.housekeeping.order.utils.ResponseEntity;
import com.hk.housekeeping.order.utils.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/order/searchSid")
public class OrderSearchBySid extends HttpServlet {
    private OrderDetailServ orderDetailServ = new OrderDetailServImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        PageUtils<List<Order>> order = new PageUtils<>();
        List<Order> list = new ArrayList<>();
        ResponseEntity<PageUtils<List<Order>>> entity = null;

        //调用业务逻辑
        try {
            Order orderContent = orderDetailServ.getOrderContent(sid);
            if (orderContent != null) {
                list.add(orderContent);         //为了配合前端的json分页解析，用数组包裹
                order.setData(list);
                order.setTotalCount(1);         //设置为1就好，因为根据订单号查询，结果是唯一的

                entity = ResponseEntity.success(order);
            } else {
                entity = ResponseEntity.error(ResponseCode.ORDER_SEARCH_ERROR);
            }
        } catch (SQLException throwables) {
            entity = ResponseEntity.error(ResponseCode.ORDER_SEARCH_ERROR);
        }

        ResponseUtils.deliver(resp, entity);
    }
}
